package cn.jinjimi.app.main.widget.biz;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import cn.jinjimi.app.R;
import cn.jinjimi.app.entity.Company;
import cn.jinjimi.app.main.adapter.extend.MyBaseAdapter;
import cn.jinjimi.app.main.widget.AdjustAbsView.AdjustGridView;

import java.util.List;

/**
 * Created by lforxeverc on 16/7/27.
 */
public class CompanyListHeaderView extends RelativeLayout implements AdapterView.OnItemClickListener {
    Context mContext;
    AdjustGridView gvCompany;
    TextView tvSetCompany;
    TextView tvQuickTips;
    List<Company> mListDatas;
    RelativeLayout rlSetView;
    OnHeaderItemSelectListener mListener;
    CompanyAdapter mAdapter;

    public CompanyListHeaderView(Context context) {
        this(context, null);
    }

    public CompanyListHeaderView(Context context, OnHeaderItemSelectListener listener) {
        super(context);
        mContext = context;
        mListener = listener;
        LayoutInflater.from(context).inflate(R.layout.view_company_header, this);
        rlSetView = (RelativeLayout) findViewById(R.id.rl_set);
        tvQuickTips = (TextView) findViewById(R.id.tv_quick_tips);
        gvCompany = (AdjustGridView) findViewById(R.id.gv_company);
        tvSetCompany = (TextView) findViewById(R.id.tv_set_company);
        gvCompany.setOnItemClickListener(this);
         mAdapter = new CompanyAdapter();
        gvCompany.setAdapter(mAdapter);

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (mListener != null && mListDatas != null && mListDatas.size() > position - 1) {
            mListener.onHeaderItemSelected(mListDatas.get(position));
        }

    }

    public void setSelectedCompany(String text) {
        rlSetView.setVisibility(VISIBLE);
        tvSetCompany.setText(text);
    }

    public void setAdapter(List<Company> mList) {
        tvQuickTips.setVisibility(View.VISIBLE);
        gvCompany.setVisibility(View.VISIBLE);
        mListDatas = mList;
        mAdapter.setItems(mList);
    }

    public interface OnHeaderItemSelectListener {
        void onHeaderItemSelected(Company company);
    }

    class CompanyAdapter extends MyBaseAdapter<Company> {
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            Holder holder;
            if (convertView == null) {
                convertView = LayoutInflater.from(mContext).inflate(R.layout.item_gv_company, null);
                holder = new Holder(convertView);
                convertView.setTag(holder);
            } else {
                holder = (Holder) convertView.getTag();
            }
            holder.tvName.setText(getItem(position).getCompanyName());
            return convertView;
        }

        class Holder {
            TextView tvName;

            Holder(View v) {
                tvName = (TextView) v.findViewById(R.id.tv_company_name);
            }
        }
    }


}
